﻿@{
    ViewBag.Title = "身份认证";
    Layout = "~/Areas/Mobile/Views/Shared/_Layout.cshtml";
    HttpCookie cookies = Request.Cookies["Expires"];
    int second = 0;
    if (cookies != null)
    {
        var timer = Convert.ToDateTime(cookies["Timer"]);
        second = 60 - (int)(DateTime.Now - timer).TotalSeconds;
    }
}
<style>
    .content {
        height: 100%;
    }
</style>
<div class="expert-content">
    <form class="expert-form">
        <div class="form-item">
            <label><span class="fa fa-mobile fa-2x"></span></label>
            <input type="text" name="mobile" id="mobile" value="" placeholder="手机号码" />
        </div>
        <div class="clear-float">
            <div class="form-item small">
                <label><span class="fa fa-envelope-o font-16"></span></label>
                <input type="text" name="code" id="code" value="" placeholder="验证码" />
            </div>
            <button type="button" class="send-code" id="send-code" onclick="SendCode()">获取</button>
        </div>
        <button type="button" class="btn-big" id="submit-btn">确定</button>
    </form>
</div>
<script>
    var sms = '@second';
    $(function () {
        if (parseInt(sms) > 0 && parseInt(sms) < 60) {
            $("#send-code").attr("disabled", true).addClass("disabled");
            startTimer(parseInt(sms));
        }
        $("#submit-btn").on("click", function () {
            submit();
        });
    });

    function SendCode() {
        var mobile = $("#mobile").val();
        if ($.trim(mobile).length == 0) {
            mui.alert("手机号码不能为空！");
            return false;
        } else {
            if (!mobile.match(/^[1][3-8]\d{9}$/)) {
                mui.alert("手机号码格式不正确！");
                return false;
            }
        }
        var data = {
            mobile: mobile
        };
        $.post("/mobile/user/getauthcode", data, function (result) {
            $("#send-code").attr("disabled", true);
            if (result.status) {
                $("#send-code").addClass("disabled");
                startTimer(60);
                mui.alert(result.data);
            }
            else {
                $("#send-code").addClass("disabled");
                startTimer(60);
                mui.alert(result.errmsg);
            }
        });
    }
    function startTimer(timerTime) {
        var timerInterval = 0;
        clearInterval(timerInterval);
        timerInterval = setInterval(function () {
            timerTime--;
            $("#send-code").text(timerTime + "s");
            if (timerTime <= 0) {
                clearInterval(timerInterval);
                $("#send-code").attr("disabled", false).removeClass("disabled").text("获取");
            }
        }, 1000);
    }

    var lock = false;
    function submit() {
        if (lock) {
            mui.alert("您点的太快了！");
        }
        else {
            lock = true;
            Loading();
            var mobile = $.trim($("#mobile").val());
            var code = $.trim($("#code").val());
            if (mobile.length == 0) {
                CloseLoading();
                lock = false;
                mui.alert("手机号码不能为空！");
                return false;
            } else {
                if (!mobile.match(/^[1][3-8]\d{9}$/)) {
                    CloseLoading();
                    lock = false;
                    mui.alert("手机号码格式不正确！");
                    return false;
                }
            }
            if (code.length == 0) {
                CloseLoading();
                lock = false;
                mui.alert("请输入您刚刚收到的验证码！");
                return false;
            }
            var data = {
                mobile: mobile,
                code: code
            };
            $.post("/mobile/user/register", data, function (result) {
                CloseLoading();
                lock = false;
                if (result.status) {
                    mui.alert(result.data, function () {
                        window.location.href = "/mobile/survey/mark";
                    });
                }
                else {
                    mui.alert(result.errmsg);
                }
            });
        }
    }
</script>
